package staticFactory.a1;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class StudentDaoImpl{

    ///如果连接不上mysql，尝试把下边最长的那句url换成下边格式( 把& 写成转义格式  &amp;  )：
    //       "jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8&amp;useUnicode=true&amp;characterEncoding=utf-8&amp;autoReconnect=true&amp;failOverReadOnly=false",

    //("jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false",
    public List<Student> findAllStudents() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
        PreparedStatement pstmt=conn.prepareStatement("select * from student");
        List<Student> list = new ArrayList<>();
        ResultSet rs=pstmt.executeQuery();
        while(rs.next()){
            Student student = new Student(rs.getString("id"),rs.getString("userName"),rs.getString("userPass"));
            list.add(student);
        }
        return list;
    }
}